
package com.loongtech.bi.action.bigame;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.game.MallStatistics10LevelManager;
import com.loongtech.bi.manager.game.MallStatisticsManager;
import com.loongtech.bi.manager.game.MallStatisticsManager.MallStatistics;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.core.util.NumberUtil;
import com.loongtech.core.util.Time;
import jodd.util.StringUtil;
import org.datanucleus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 游戏内分析- 商城统计Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/game/mallStatisticsGame")
public class MallStatisticsBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private Map<String, String> reasonItmes = GameParamConfigManager.getItems(CommonGameParam.MALL_NAME); // 商城名称
	private Map<String, String> reasonItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_NAME); // 商城名称
	private Map<String, String> currencytypeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_SEARCH_CURRENCYTYPE); // 商城货币类型
	private Map<String, String> typeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE);  // 物品类型反转
	private List<MallStatistics> dataList1;
	private List<MallStatistics> dataList2;
	private String param;
	private Integer rolelevel;
	private Integer reason;
	private Map <String,Integer> rolelevelMap;
	@Autowired
	private MallStatisticsManager mallStatisticsManager;
	@Autowired
	private MallStatistics10LevelManager mallStatistics10LevelManager;
	
	// 查询物品名称
	public String getItemType(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (typeItmesRevise.get(key) == null) ? key : typeItmesRevise.get(key); 
	}
	
	//获得商城货币类型
	public String getMallCurrencytype(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (currencytypeItmesRevise.get(key) == null) ? key : currencytypeItmesRevise.get(key); 
	}
	
	//获得商城名称
	public String getMallName(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (reasonItmesRevise.get(key) == null) ? key : reasonItmesRevise.get(key); 
	}
	
	//货币消耗量
	public Long getSumchangenum(Long sumchangenum) {
		return -sumchangenum; 
	}
	

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
	    try {
            reasonItmes.remove("全部");
            currencytypeItmesRevise.remove(-99999);
            setRolelevelMap();

            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            //result.put("channelItmes", channelItmes);
            //result.put("platformItmes", platformItmes);

            result.put("reasonItmes", reasonItmes);
            result.put("rolelevelMap", rolelevelMap);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }


    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String server, @RequestParam String param, @RequestParam String reason, @RequestParam String rolelevel, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryServer = server;
            this.rolelevel = Integer.parseInt(rolelevel);
            this.param = param;
            this.reason = Integer.parseInt(reason);

            if (dateStart.getTime() > dateEnd.getTime()) {
                result.put("res", "1");
                result.put("msg", "开始时间不能大于结束时间!");
                printToJson(response, result, callback);
                return;
            }
            if (!StringUtil.isBlank(param) && !NumberUtil.isNumber(param)) {
                result.put("res", "1");
                result.put("msg", "物品ID不是有效数字!");
                printToJson(response, result, callback);
                return;
            }
            String shangchengReason = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.SHANGCHENG_REASON).keySet().toArray()[1];
            if (StringUtils.isEmpty(shangchengReason) || GameParamConfigManager.ALL_FLAT.equals(shangchengReason)) {
                result.put("res", "1");
                result.put("msg", "没有配置 64 消耗原因是商城 的值 !");
                printToJson(response, result, callback);
                return;
            }

            dataList1 = new ArrayList<>();

            StringBuilder currencytype = new StringBuilder();
            currencytype.append("(");

            int sum = 0;

            //货币类型传递
            for(Map.Entry<String,String> entry : currencytypeItmesRevise.entrySet()) {
                String type = entry.getKey();

                if(sum == 0) {
                    currencytype.append(type);
                }else{
                    currencytype.append("," + type);
                }

                sum ++;
            }

            currencytype.append(")");

            dataList1 = mallStatisticsManager.getMallStatistics(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,this.param,this.rolelevel,currencytype.toString(), shangchengReason);

            result.put("res", "0");
            result.put("dataList1", dataList1);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }
	
	//根据最后的日期更新角色等级
	public void updateRolelevel() {
		setRolelevelMap();
	}
	
	//设置等级map
	private void setRolelevelMap() {
		rolelevelMap = new LinkedHashMap<String,Integer>();
		rolelevelMap.put("无筛选等级", -1);
		
		List<Object[]> list = mallStatistics10LevelManager.getMallStatistics10LevelByDateEnd(dateStart,dateEnd);
		
		for(Object o :list) {
			int level = NumberUtil.parseInt(o);
			
			int beginLevel = level * 10 + 1;
			//
			int endLevel = (level + 1) * 10;
			
			String str = beginLevel + "-" + endLevel + " 等级";
			
			rolelevelMap.put(str,level);
		}
		
		rolelevelMap.put("总体", -2);
	}
	
	//等级名称
	public String getLevelName(Integer rolelevel) {
		if(rolelevel == -1) {
			return "总体";
		}
		
		int level = NumberUtil.parseInt(rolelevel);
		
		int beginLevel = level * 10 + 1;
		//
		int endLevel = (level + 1) * 10;
		
		return beginLevel + "-" + endLevel + "等级";
	}
	
	//获得元宝的占比
	public String getYuanBaoProportion(Long sumchangenum,Long allSumchangenum) {
		return PageHelper.double2Percent(sumchangenum.doubleValue() / allSumchangenum.doubleValue());
	}
	
	//获得角色的占比
	public String getRoleProportion(Integer rolenum,Integer allRolenum) {
		return PageHelper.double2Percent(rolenum.doubleValue() / allRolenum.doubleValue());
	}

    @Override
    public void search() {

    }

    @Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<MallStatistics> getDataList1() {
		return dataList1;
	}

	public void setDataList1(List<MallStatistics> dataList1) {
		this.dataList1 = dataList1;
	}


	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}


	public Integer getRolelevel() {
		return rolelevel;
	}


	public void setRolelevel(Integer rolelevel) {
		this.rolelevel = rolelevel;
	}


	public Map <String,Integer> getRolelevelMap() {
		return rolelevelMap;
	}


	public void setRolelevelMap(Map <String,Integer> rolelevelMap) {
		this.rolelevelMap = rolelevelMap;
	}


	public List<MallStatistics> getDataList2() {
		return dataList2;
	}


	public void setDataList2(List<MallStatistics> dataList2) {
		this.dataList2 = dataList2;
	}

	public Map<String, String> getReasonItmes() {
		return reasonItmes;
	}

	public void setReasonItmes(Map<String, String> reasonItmes) {
		this.reasonItmes = reasonItmes;
	}

	public Integer getReason() {
		return reason;
	}

	public void setReason(Integer reason) {
		this.reason = reason;
	}
}
